<template>
  <div class="login">
    <div class="form">
      <div class="left">
        <!-- <video src="@/assets/video/video.mp4" muted loop autoplay></video> -->
        <img src="@/assets/images/blue2.png" alt="">
        <!-- <img src="@/assets/images/blue1.jpg" alt=""> -->
      </div>
      <div class="right">
        <div class="right-con">
          <h3 class="title">ssh后台管理系统</h3>
          <el-form 
            v-show="formType==='login'"
            ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <!-- 为不同的表单item项通过`prop`属性，指定不同的校验规则，进行表单的验证 -->
            <el-form-item prop="userAccount" label="用户名或邮箱">
              <el-input 
              class="textarea"
              v-model="loginForm.userAccount" type="text" auto-complete="off" placeholder="用户名或邮箱" >
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="pwd"  label="密码">
              <el-input
                class="textarea"
                v-model="loginForm.pwd"
                type="password"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <!-- <el-form-item prop="code" v-if="captchaOnOff">
              <el-input
                class="textarea"
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="login-code"
              style="width: 37%">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </el-form-item> -->
            <div class="checkbox-div">
              <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 0px 0px;">
                记住密码
              </el-checkbox>
              <!-- <router-link class="register-button" :to="'/register'">没有账号？点击注册</router-link> -->
              <!-- <a href="#" class="register-button" @click.prevent="handleRegister">没有账号？点击注册</a> -->
              <!-- <a href="#">ene</a> -->
            </div>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width:100%;"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div  v-if="register">
                <router-link style="float: left;" class="link-type" :to="'/registerQQMail'">使用QQ邮箱注册</router-link>
                <router-link style="float: right;" class="link-type" :to="'/register'">没有账号？点击注册</router-link>
              </div>
            </el-form-item>
          </el-form>
          <!-- <el-form 
            v-show="formType==='register'"
            ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
            <el-form-item prop="userAccount">
              <el-input 
              class="textarea"
              v-model="loginForm.userAccount" type="text" auto-complete="off" placeholder="账号" >
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="pwd">
              <el-input
                class="textarea"
                v-model="loginForm.pwd"
                type="password"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
          </el-form> -->
        </div>
        
      </div>    
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      formType:"login",
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        // username: "ssh",
        userAccount: 'ssh',
        pwd: "123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        userAccount: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        pwd: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  // 侦听
  watch: {
    // 监听$route的变化，一旦对象的任意属性发生变化都会执行handler方法。
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    // 获取验证码图片
    this.getCode();
    this.getCookie();
  },
  methods: {
    // 获取验证码
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          // 验证码开关为开
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    // 从cookie中拿到loginForm 数据
    getCookie() {
      // const username = Cookies.get("username");
      const userAccount = Cookies.get("userAccount");
      const pwd = Cookies.get("pwd");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        userAccount: userAccount === undefined ? this.loginForm.userAccount : userAccount,
        pwd: pwd === undefined ? this.loginForm.pwd : decrypt(pwd),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        code: '10' // 给一个默认值，否则接口会报错
      };
    },
    // 登录
    handleLogin() {
      console.log('loginForm为:');
      console.log(this.loginForm);
      // 验证
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          // 记住密码的操作
          if (this.loginForm.rememberMe) {
            // Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("userAccount", this.loginForm.userAccount, { expires: 30 });
            Cookies.set("pwd", encrypt(this.loginForm.pwd), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            // Cookies.remove("username");
            Cookies.remove("userAccount");
            Cookies.remove("pwd");
            Cookies.remove('rememberMe');
          }
          // 使用dispatch()调用action
          this.$store.dispatch("Login", this.loginForm).then(() => {
            //通过代码的方式修改路由
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            // 验证码开关开的了的话
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },
    // 注册
    handleRegister() {
      console.log("注册");
      this.formType = 'register'
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped="scoped">
.login {
  // display: flex;
  // justify-content: center;
  // align-items: center;
  // height: 100%;
  // background-image: url("../assets/images/login-background.jpg");
  // background-size: cover;

  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(
    to left top,
    #d16ba5,
    #c777b9,
    #ba83ca,
    #aa8fd8,
    #9a9ae1,
    #8aa7ec,
    #79b3f4,
    #69bff8,
    #52cffe,
    #41dfff,
    #46eefa,
    #5ffbf1
  );
}
.form {
  // border-radius: 6px;
  // background: #ffffff;
  // width: 400px;
  // padding: 25px 25px 5px 25px;
  width: 900px;
  height: 560px;
  display: flex;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.left {
  width: 500px;
  height: 560px;
}

.left img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.right {
  width: 400px;
  height: 560px;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  justify-content: center;
  align-items: center;
}
.right-con {
  // width: 70%;
  display: flex;
  flex-direction: column;
  // text-align: center;
}
.title {
  // margin: 0px auto 30px auto;
  // text-align: center;
  // color: #707070;
  font-size: 26px;
  color: #70b4e3;
  font-weight: 400;
  padding-bottom: 10px;
}
// h3 {
//   font-size: 12px;
//   font-weight: 400;
//   color: #70b4e3;
//   padding: 20px 0;
// }
.el-form-item {
  width: 100%;
  // width:500px;
  outline: none;
  border-style: none;
  // border: 1px solid #ffffff;
  // border-bottom: 1px solid #70b4e3;
  color: #366ae6;
  background-color: rgba(0, 0, 0, 0);
}
.textarea ::v-deep .el-input__inner {
  width: 100%;
  outline: none;
  border: none;
  border-bottom: 1px solid #70b4e3;
  color: #366ae6;
  background-color: rgba(0, 0, 0, 0);
}
// .login-tip {
//   font-size: 13px;
//   text-align: center;
//   color: #bfbfbf;
// }
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.checkbox-div {
  overflow: hidden;
  margin-top: 5px;
}
.checkbox-div ::v-deep .el-checkbox {
  float: left;
  // width: 100%;
  color: #70b4e3;
}
.register-button {
  float: right;
  z-index: 99;
  font-size: 14px;
  color: rgb(24, 144, 255);
  text-decoration: underline;
}


.el-button {
  width: 100%;
  height: 40px;
  border-radius: 20px;
  border: none;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  margin-top: 50px;
  background-image: linear-gradient(120deg, #76daec 0%, #c5a8de 100%);
}
// .link-type {
//   margin-top: 10px;
// }
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  // color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
<style rel="stylesheet/scss" lang="scss">

</style>